.POSIX:

#
# DO NOT EDIT THIS FILE. YOUR CHANGES MAY GET OVERWRITTEN.
# Edit Makefile.in instead and then re-generate the Makefile
# with python3 -mmacrop Makefile.in Makefile.
#


#
# Special targets
#

.PHONY: \
	check \
	clean \
	dependcheck \
	dist \
	distcheck \
	distclean \
	deb \
	debupload \
	docs \
        install \
	install-comp \
	install-man \
	install-strip \
	install-module \
	installdirs \
	lint \
	maintainer-clean \
	mostlyclean \
	virtinstall \
	wheelupload \
	pre-install \
	post-uninstall \
	quickcheck \
	quicklint \
	uninstall \
	uninstall-module \
	uninstallfiles \
	userinstall

.SUFFIXES:

all:
	:


#
# Macros
#

BANDIT = bandit
BANDITFLAGS = -qc pyproject.toml
CTAGS = ctags
DESTDIR =
DPKG_DEB = dpkg-deb
FLAKE8 = flake8
GPG = gpg -ab
GPGFLAGS = -q --batch --yes
SHELL = /bin/sh
INSTALL = install
LINTIAN = lintian
# TODO: Add pedantic and warning, once changelos are to spec.
LINTIANFLAGS = --cfg deb/lintian.cfg --fail-on error
MYPY = mypy
PATHCHK = pathchk
PYLINT = pylint
PYRIGHT = pyright
PYTHON = python3
PIP = pip
PIPFLAGS = --root=$(DESTDIR) --prefix=$(prefix)
PREFIX = /usr/local
PYPI_UPLOAD = $(TWINE) upload $(TWINEFLAGS)
RUFF = ruff
RUFFFLAGS = --quiet
SPHINX = sphinx-build
VENV = $(PYTHON) -m venv
VERMIN = vermin
VULTURE = vulture
VPATH = .
SAFETY = safety
SAFETYFLAGS = --disable-optional-telemetry --policy-file=$(safetypolicy)
TAR = pax -ws '/^/$(distname)\//' -x ustar
TWINE = twine
TWINEFLAGS = -s -rtest-pypi
UNTAR = tar -x
UNZIP = gunzip -c 
ZIP = gzip

bashcompdir = $(datarootdir)/bash-completion/completions
bindir = $(exec_prefix)/bin
checks = discover
copyright = COPYRIGHT
datarootdir = $(prefix)/share
debdir = deb/$(package)_$(version)-0_all
deburl = $(forgejo_url)/debian/pool/bullseye/main/upload
deb = $(debdir).deb
distar = $(distname).tgz
distfiles = \
	INSTALL.rst \
	LICENSE.rst \
	NEWS.rst \
	README.rst \
	comp/sievemgr.bash \
	comp/sievemgr.zsh \
	deb/control.in \
	deb/copyright.in \
	deb/lintian.cfg \
	docs/Makefile \
	docs/_static/custom.css \
	docs/changelog.rst \
	docs/command.rst \
	docs/conf.py \
	docs/config.rst \
	docs/contrib.rst \
	docs/feedback.rst \
	docs/index.rst \
	docs/install.rst \
	docs/license.rst \
	docs/module.rst \
	docs/security.rst \
	docs/sphinx.txt \
	examples/sievepatch \
	linters.txt \
	Makefile \
	Makefile.in \
	macrop.py \
	man/sieve.cf.5 \
	man/sievemgr.1 \
	pyproject.toml \
	setup.cfg \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py

distname = $(module)-$(version)
exec_prefix = $(prefix)
forgejo_url = https://codeberg.org/api/packages/$(forgejo_user)
forgejo_user = odkr
installdirs = \
	$(DESTDIR)$(man1dir) \
	$(DESTDIR)$(man5dir) \
	$(DESTDIR)$(bashcompdir) \
	$(DESTDIR)$(zshcompdir)

instlog = install.log
mandir = $(datarootdir)/man
man1dir = $(mandir)/man1
man5dir = $(mandir)/man5
man1ext = .1
man5ext = .5
module = $(package)
package = ${__name__}
prefix = $(PREFIX)
safetypolicy = safety.yml
srcs = \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py

venvdir = /opt/odkr/$(module)
version = ${__version__}
zshcompdir = $(datarootdir)/zsh-completions


#
# Build system
#

.SUFFIXES: .in

Makefile: sievemgr.py Makefile.in

.in:
	$(PYTHON) -mmacrop $@.in $@


#
# Installation
#

install: pre-install install-module install-man install-comp

install-man: \
	installdirs \
	$(DESTDIR)$(man1dir)/sievemgr$(man1ext) \
	$(DESTDIR)$(man5dir)/sieve.cf$(man5ext)

uninstall: uninstall-module uninstallfiles uninstalldirs post-uninstall

install-comp: \
	installdirs \
	$(DESTDIR)$(bashcompdir)/sievemgr.bash \
	$(DESTDIR)$(zshcompdir)/_sievemgr

install-strip: install

virtinstall: $(venvdir)/bin/pip

install-module:
	$(NORMAL_INSTALL)
	$(PIP) install $(PIPFLAGS) .
	printf 'pip: %s\n' '$(PIP)' >>$(instlog)

pre-install:
	$(PRE_INSTALL)
	set -C && : >$(instlog)

installdirs: $(installdirs)

userinstall:
	$(MAKE) -e PIPFLAGS=--user prefix="$$$$HOME/.local" install

virtinstall:
	@echo 'Installing SieveManager to $(venvdir) ...'
	$(MAKE) -e PIP=$(venvdir)/bin/pip PIPFLAGS= \
		PREFIX='$(venvdir)' install

uninstall-module:
	$(NORMAL_UNINSTALL)
	-"$$$$(sed -n 's/pip: //p' $(instlog))" uninstall -y $(module)

uninstalldirs:
	$(POST_UNINSTALL)
	- sed -n 's/^dir: //p' $(instlog) | xargs -E '' rmdir -pv

uninstallfiles:
	$(POST_UNINSTALL)
	sed -n 's/^file: //p' $(instlog) | xargs -E '' rm -f

post-uninstall:
	$(POST_UNINSTALL)
	rm $(instlog)

$(DESTDIR)$(man1dir)/sievemgr$(man1ext): man/sievemgr.1
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sievemgr.1 '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(man5dir)/sieve.cf$(man5ext): man/sieve.cf.5
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sieve.cf.5 '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(bashcompdir)/sievemgr.bash: comp/sievemgr.bash
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(zshcompdir)/_sievemgr: comp/sievemgr.zsh
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.zsh '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(installdirs):
	$(PRE_INSTALL)
	mkdir -pm0755 $@
	printf 'dir: %s\n' '$@' >>$(instlog)

$(venvdir)/bin/pip:
	@echo 'Creating a Python virtual environment in $(venvdir) ...'
	$(PYTHON) -mvenv $(venvdir)


#
# Documentation
#

docs: man/sievemgr.1 man/sieve.cf.5

man/sievemgr.1: docs/command.rst

man/sieve.cf.5: docs/config.rst

man/sievemgr.1.gz: man/sievemgr.1

man/sieve.cf.5.gz: man/sieve.cf.5

man/sievemgr.1 man/sieve.cf.5:
	. ./bin/activate && $(SPHINX) $(SPHINXFLAGS) -bman docs man


#
# Distribution
#

dist: dependcheck $(distar) $(distar).asc

distcheck: dist

$(distar): lint check $(distfiles)

$(distar).asc: $(distar)

dependcheck:
	$(SAFETY) scan $(SAFETYFLAGS) || [ $$$$? -eq 127 ]

distcheck:
	$(UNZIP) $(distar) | $(UNTAR)
	cd $(distname) && $(MAKE) -e check
	rm -rf $(distname)

$(distar):
	$(PATHCHK) $(PATHCHKFLAGS) -Pp $(distfiles)
	$(TAR) $(TARFLAGS) $(distfiles) | $(ZIP) $(ZIPFLAGS) >$(distar)

$(distar).asc:
	$(GPG) $(GPGFLAGS) $(distar)


#
# PyPI
#

build: lint check
	$(PYTHON) -m build

wheelupload: build
	$(PYPI_UPLOAD) dist/$(distname).tar.gz dist/$(distname)-py3-none-any.whl


#
# Debian package
#

deb: $(deb)

debupload: $(deb)

$(deb): \
		lint \
		check \
		$(debdir)/usr/lib/python3/dist-packages/sievemgr.py \
		$(debdir)/usr/share/man/man1/sievemgr.1.gz \
		$(debdir)/usr/share/man/man5/sieve.cf.5.gz \
		$(debdir)/usr/share/bash-completion/completions/sievemgr.bash \
		$(debdir)/usr/share/zsh-completions/_sievemgr \
		$(debdir)/DEBIAN/control \
		$(debdir)/usr/share/doc/$(package)/copyright \
		$(debdir)/usr/share/doc/$(package)/changelog.gz
	$(DPKG_DEB) $(DPKG_DEBFLAGS) --root-owner-group --build $(debdir)
	$(LINTIAN) $(LINTIANFLAGS) $@ || [ $$$$? -eq 127 ]
	GPG_TTY=$$$$(tty) debsigs --sign=maint $@

$(debdir)/usr/lib/python3/dist-packages/sievemgr.py:
	/usr/bin/pip3 install --root=$(debdir) --prefix=/usr --no-deps .
	if [ -e $(debdir)/usr/local ]; \
	then \
		mv $(debdir)/usr/local/* $(debdir)/usr; \
		rmdir $(debdir)/usr/local; \
	fi
	pydir="$$$$(ls -d $(debdir)/usr/lib/python*)"; \
	debpydir="$$$${pydir%.*}"; \
	if [ -e "$$$$pydir" ]; \
	then mv "$$$$pydir" "$$$$debpydir"; \
	fi
	if [ -e "$$$$debpydir/site-packages" ]; \
	then mv "$$$$debpydir/site-packages" "$$$$debpydir/dist-packages"; \
	fi
	find $(debdir) -type d -name __pycache__ -exec rm -rf '{}' ';' -prune

$(debdir)/usr/share/man/man1/sievemgr.1.gz: man/sievemgr.1
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <man/sievemgr.1 >$@

$(debdir)/usr/share/man/man5/sieve.cf.5.gz: man/sieve.cf.5
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <man/sieve.cf.5 >$@

$(debdir)/DEBIAN/control: deb/control
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r  deb/control $@

$(debdir)/usr/share/doc/$(package)/copyright: deb/copyright
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r deb/copyright $@

$(debdir)/usr/share/doc/$(package)/changelog.gz: NEWS.rst
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <NEWS.rst >$@

$(debdir)/usr/share/bash-completion/completions/sievemgr.bash:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash $@

$(debdir)/usr/share/zsh-completions/_sievemgr:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.zsh $@

deb/control: sievemgr.py deb/control.in

deb/copyright: sievemgr.py deb/copyright.in

debupload:
	GPG_TTY=$$$$(tty) debsigs --sign=origin $(deb)
	curl -n --netrc-file .netrc -T$(deb) $(deburl)


#
# Cleanup
#

clean: mostlyclean

distclean: clean

maintainer-clean: distclean

clean:
	rm -rf $(package).egg-info .*_cache build deb/$(package)_* 

distclean:
	rm -f $(instlog) tags

maintainer-clean:
	@echo '$@ is for maintainers only!'
	@echo 'It deletes files that cannot be built with standard tools.'
	rm -f deb/control deb/copyright Makefile man/sievemgr.1 man/sieve.cf.5

mostlyclean:
	find . -type d -name __pycache__ | xargs -E '' rm -rf
	find . -type f -name '*.bak' -exec rm '{}' +
	rm -f $(package)-*.tgz $(package)-*.tgz.asc deb/$(package)_*.deb dist/*


#
# Tags
#

TAGS: tags

tags: $(srcs)
	$(CTAGS) $(CTAGSFLAGS) $(srcs)


#
# Tests
#

check:
	$(PYTHON) -m unittest $(checks)

quickcheck:
	QUICKTEST=y $(PYTHON) -m unittest $(checks)


#
# Linters
#

lint: quicklint

lint:
	$(PYRIGHT) $(PYRIGHTFLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(FLAKE8) $(FLAKE8FLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(VERMIN) $(VERMINFLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(RUFF) check $(RUFFFLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(PYLINT) $(PYLINTFLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(VULTURE) $(VULTUREFLAGS) $(srcs) || [ $$$$? -eq 127 ]

quicklint:
	$(MYPY) $(MYPYFLAGS) $(srcs) || [ $$$$? -eq 127 ]
	$(BANDIT) $(BANDITFLAGS) $(srcs) || [ $$$$? -eq 127 ]

